package bsi.vn.Lib;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import bsi.vn.Lib.Consts;

public class DBAdapter {

	private Context context;
	private SQLiteDatabase database;
	private DBHelper dbHelper;

	public DBAdapter(Context context) {
		this.context = context;
	}

	public DBAdapter open() throws SQLException {
		dbHelper = new DBHelper(context);
		database = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		dbHelper.close();
	}
	
	//START PROVINCE
	public long insertProvinceTbl(int id, String name) {
		ContentValues initialValues = ProvinceValues(id, name);
		return database.insert(Consts.TABLE_PROVINCE, null, initialValues);
	}
	
	public Cursor getAllProvinceData() {
		return database.query(Consts.TABLE_PROVINCE, new String[] { Consts.DBF_ROWID,
				Consts.DBF_PROVINCE}, null, null, null,null, null);
	
	}
	
	private ContentValues ProvinceValues(int id, String name) {
		ContentValues values = new ContentValues();
		values.put(Consts.DBF_ROWID, id);
		values.put(Consts.DBF_PROVINCE, name);
		return values;
	}
	//END PROVINCE
	
	//START CUSTOMER
	public long insertCustomerTbl(int id, String name) {
		ContentValues initialValues = CustomerValues(id, name);
		return database.insert(Consts.TABLE_CUSTOMER, null, initialValues);
	}
		
	public Cursor getAllCustomerData() {
		return database.query(Consts.TABLE_CUSTOMER, new String[] { Consts.DBF_ROWID,
			Consts.DBF_CUSTOMER}, null, null, null,null, null);
		
	}
		
	private ContentValues CustomerValues(int id, String name) {
		ContentValues values = new ContentValues();
		values.put(Consts.DBF_ROWID, id);
		values.put(Consts.DBF_CUSTOMER, name);
		return values;
	}
	//END CUSTOMER
	
	//START ERRORLIST
	public long insertErrorListTbl(int id, String errorcode, String description) {
		ContentValues initialValues = ErrorListValues(id, errorcode, description);
		return database.insert(Consts.TABLE_ERRORLIST, null, initialValues);
	}
			
	public Cursor getAllErrorListData() {
		return database.query(Consts.TABLE_ERRORLIST, new String[] { Consts.DBF_ROWID,
		Consts.DBF_ERRORCODE, Consts.DBF_DESCRIPTION}, null, null, null,null, null);
	}
			
	private ContentValues ErrorListValues(int id, String errorcode, String description) {
		ContentValues values = new ContentValues();
		values.put(Consts.DBF_ROWID, id);
		values.put(Consts.DBF_ERRORCODE, errorcode);
		values.put(Consts.DBF_DESCRIPTION, description);
		return values; 
	}
	//END ERRORLIST

	//START ERRORDETAIL
	public long insertErrorDetailTbl(int id, int errorid, int order, String description) {
		ContentValues initialValues = ErrorDetailValues(id, errorid, order, description);
		return database.insert(Consts.TABLE_ERRORDETAIL, null, initialValues);
	}
				
	public Cursor getAllErrorDetailData() {
		return database.query(Consts.TABLE_ERRORDETAIL, new String[] { Consts.DBF_ROWID,
		Consts.DBF_ERRORID, Consts.DBF_ORDER, Consts.DBF_DESCRIPTION}, null, null, null,null, null);
	}
	
	public Cursor getErrorDetailByErrorId(int errorid) {
		return database.query(Consts.TABLE_ERRORDETAIL, new String[] { Consts.DBF_ROWID,
		Consts.DBF_ERRORID, Consts.DBF_ORDER, Consts.DBF_DESCRIPTION}, Consts.DBF_ROWID + "=" + errorid, null, null,null, Consts.DBF_ORDER + " ASC");
	}
				
	private ContentValues ErrorDetailValues(int id, int errorid, int order, String description) {
		ContentValues values = new ContentValues();
		values.put(Consts.DBF_ROWID, id);
		values.put(Consts.DBF_ERRORID, errorid);
		values.put(Consts.DBF_ORDER, order);
		values.put(Consts.DBF_DESCRIPTION, description);
		return values;
	}
	//END ERRORDETAIL

	//START ALL STOCK
	public long insertCookieTbl(int userid, int usertype, String cookie, String ipaddress) {
		ContentValues initialValues = CookieValues(userid, usertype, cookie, ipaddress);
		return database.insert(Consts.TABLE_COOKIE, null, initialValues);
	}

	public boolean updateCookieTbl(long rowId, int userid, int usertype, String cookie, String ipaddress) {
		ContentValues updateValues = CookieValues(userid, usertype, cookie, ipaddress);
		return database.update(Consts.TABLE_COOKIE, updateValues, Consts.DBF_ROWID + "="
				+ rowId, null) > 0;
	}

	public boolean deleteCookieTbl(long rowId) {
		return database.delete(Consts.TABLE_COOKIE, Consts.DBF_ROWID + "=" + rowId, null) > 0;
	}
	
//	public boolean deleteAllStockByCode(String code) {
//		return database.delete(TABLE_ALLSTOCK, KEY_CODE + "=" + code, null) > 0;
//	}
//
	public Cursor getCookie() {
		return database.query(Consts.TABLE_COOKIE, new String[] {Consts.DBF_USERID, Consts.DBF_USERTYPE, Consts.DBF_COOKIE, Consts.DBF_IPADDRESS}, null, null, null,null, null);
	
	}
	
//	public int countAllStock() {
//		int count = 0;
//		Cursor cursor = database.query(TABLE_ALLSTOCK, new String[]{KEY_ROWID}, null, null, null,null,null);
//		if (cursor != null ) {
//			if  (cursor.moveToFirst()) {
//				do {
//					count = count + 1;
//				}while (cursor.moveToNext());
//			} 
//		}
//		return count;
//	}
//
//	public Cursor getAllStockById(long rowId) throws SQLException {
//		Cursor mCursor = database.query(true, TABLE_ALLSTOCK, new String[] {
//				KEY_ROWID, KEY_CODE, KEY_MARKET},
//				KEY_ROWID + "=" + rowId, null, null, null, null, null);
//		if (mCursor != null) {
//			mCursor.moveToFirst();
//		}
//		return mCursor;
//	}
//	
//	public Cursor getAllStockByCode(String Code) throws SQLException {
//		Cursor mCursor = database.query(true, TABLE_ALLSTOCK, new String[] {
//				KEY_ROWID, KEY_CODE, KEY_MARKET},
//				KEY_CODE + "=" + Code, null, null, null, null, null);
//		if (mCursor != null) {
//			mCursor.moveToFirst();
//		}
//		return mCursor;
//	}
//	
	private ContentValues CookieValues(int userid, int usertype, String cookie, String ipaddress) {
		ContentValues values = new ContentValues();
		values.put(Consts.DBF_USERID, userid);
		values.put(Consts.DBF_COOKIE, cookie);
		values.put(Consts.DBF_IPADDRESS, ipaddress);
		values.put(Consts.DBF_USERTYPE, usertype);
		return values;
	}
	//END ALL STOCK
	
	//START LOCATION
	public long createLocationTbl(String code, int market, double last, double ceiling, double floor, double change, int vol) {
		ContentValues initialValues = FavoriteValues(code, market, last, ceiling, floor, change, vol);
		return database.insert(Consts.TABLE_LOCATION, null, initialValues);
	}

	public boolean updateLocationTbl(long rowId, String code, int market, double last, double ceiling, double floor, double change, int vol) {
		ContentValues updateValues = FavoriteValues(code, market, last, ceiling, floor, change, vol);
		return database.update(Consts.TABLE_LOCATION, updateValues, Consts.DBF_ROWID + "="
				+ rowId, null) > 0;
	}
	
//	public Cursor getAllFavorites() {
//		return database.query(TABLE_FAVORITES, new String[] { KEY_ROWID,
//				KEY_CODE, KEY_MARKET, KEY_LAST, KEY_CEILING, KEY_FLOOR, KEY_CHANGE, KEY_VOL}, null, null, null,null, KEY_CODE);
//	
//	}
//
//	public boolean deleteFavorites(long rowId) {
//		return database.delete(TABLE_FAVORITES, KEY_ROWID + "=" + rowId, null) > 0;
//	}
//	
//	public boolean deleteFavoritesByCode(String Code) {
//		return database.delete(TABLE_FAVORITES, KEY_CODE + "='" + Code + "'", null) > 0;
//	}
//	
//	public boolean stockExist(String Code) throws SQLException {
//		String count = "SELECT count(*) FROM " + TABLE_FAVORITES + " WHERE " + KEY_CODE + " like '"+Code+"'";     
//		Cursor mcursor = database.rawQuery(count, null); 
//		mcursor.moveToFirst();
//		int icount = mcursor.getInt(0); 
//		if (icount > 0)
//			return true;
//		else
//			return false;
//	}
//	
	private ContentValues FavoriteValues(String code, int market, double last, double ceiling, double floor, double change, int vol) {
		ContentValues values = new ContentValues();
		values.put(Consts.DBF_CODE, code);
		values.put(Consts.DBF_MARKET, market);
		values.put(Consts.DBF_LAST, last);
		values.put(Consts.DBF_CEILING, ceiling);
		values.put(Consts.DBF_FLOOR, floor);
		values.put(Consts.DBF_CHANGE, change);
		values.put(Consts.DBF_VOL, vol);
		return values;
	}
//	//END FAVORITES
//	
//	//START INVEST LIST
//	public long createInvestList(String code, int market, double bprice, int vol, long bdate) {
//		ContentValues initialValues = investListValues(code, market, bprice, vol, bdate);
//		return database.insert(TABLE_INVESTLIST, null, initialValues);
//	}
//
//	public boolean updateInvestList(long rowId, String code, int market, double bprice, int vol, long bdate) {
//		ContentValues updateValues = investListValues(code, market, bprice, vol, bdate);
//		return database.update(TABLE_INVESTLIST, updateValues, KEY_ROWID + "="
//				+ rowId, null) > 0;
//	}
//	
//	public boolean updateInvestVol(int rowId, int vol) {
//		ContentValues values = new ContentValues();
//		values.put(KEY_VOL, vol);
//		return database.update(TABLE_INVESTLIST, values, KEY_ROWID + "="
//				+ rowId, null) > 0;
//	}
//	
//	public Cursor getAllInvestList() {
//		return database.query(TABLE_INVESTLIST, new String[] { KEY_ROWID,
//				KEY_CODE, KEY_MARKET, KEY_BPRICE, KEY_VOL, KEY_BDATE}, null, null, null,null, KEY_CODE);
//	
//	}
//
//	public boolean deleteInvestList(long rowId) {
//		return database.delete(TABLE_INVESTLIST, KEY_ROWID + "=" + rowId, null) > 0;
//	}
//	
//	private ContentValues investListValues(String code, int market, double bprice, int vol, long bdate) {
//		ContentValues values = new ContentValues();
//		values.put(KEY_CODE, code);
//		values.put(KEY_MARKET, market);
//		values.put(KEY_BPRICE, bprice);
//		values.put(KEY_VOL, vol);
//		values.put(KEY_BDATE, bdate);
//		return values;
//	}
//	//END INVEST LIST
//	
//	//START INVEST RESULT
//	public long createInvestResult(String code, int market, double bprice, double sprice, int vol, double value, long sdate) {
//		ContentValues initialValues = investResultValues(code, market, bprice, sprice, vol, value, sdate);
//		return database.insert(TABLE_INVESTRESULT, null, initialValues);
//	}
//
//	public boolean updateInvestResult(long rowId, String code, int market, double bprice, double sprice, int vol, double value, long sdate) {
//		ContentValues updateValues = investResultValues(code, market, bprice, sprice, vol, value, sdate);
//		return database.update(TABLE_INVESTRESULT, updateValues, KEY_ROWID + "="
//				+ rowId, null) > 0;
//	}
//
//	public Cursor getAllInvestResult() {
//		return database.query(TABLE_INVESTRESULT, new String[] { KEY_ROWID,
//				KEY_CODE, KEY_MARKET, KEY_BPRICE, KEY_SPRICE, KEY_VOL, KEY_VALUE,KEY_SDATE}, null, null, null,null, KEY_CODE);
//	
//	}
//	
//	public boolean deleteInvestResult(long rowId) {
//		return database.delete(TABLE_INVESTRESULT, KEY_ROWID + "=" + rowId, null) > 0;
//	}
//	
//	private ContentValues investResultValues(String code, int market, double bprice, double sprice, int vol, double value, long sdate) {
//		ContentValues values = new ContentValues();
//		values.put(KEY_CODE, code);
//		values.put(KEY_MARKET, market);
//		values.put(KEY_SPRICE, sprice);
//		values.put(KEY_BPRICE, bprice);
//		values.put(KEY_VOL, vol);
//		values.put(KEY_VALUE, value);
//		values.put(KEY_SDATE, sdate);
//		return values;
//	}
//	//END INVEST RESULT
//	
	public boolean deleteTable(String TableName){
		if (database != null) {
			database.execSQL("DELETE FROM " + TableName);
			return true;
		}
		return false;
	}

}